﻿@using Newtouch.Infrastructure;
@using Newtouch.Common.Operator;
@{
    ViewBag.Title = "ReportLossAndProfit";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<input hidden id="OrganizeId" value="@OperatorProvider.GetCurrent().OrganizeId">
<form id="form1">
    <div class="panel panel-default" style="margin-top:10px;">
        <div class="panel-heading">
            损益信息
        </div>
        <div style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle">单据号：</th>
                    <td class="formValue">
                        <input type="text" id="txtSYDH" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">损溢情况：</th>
                    <td style="padding-left:12px;">
                        <div class="btn-group" id="btnLossAndProfitTypeGourp" style="margin-top:0;">
                            <button type="button" id="btnLoss" data-keywordtype="Loss" class="btn active btn-restore">报损</button>
                            <button type="button" id="btnProfit" data-keywordtype="Profit" class="btn btn-default">报溢</button>
                        </div>
                    </td>
                    <th class="formTitle">损溢总额(元)：</th>
                    <td class="formValue">
                        <input type="text" id="txtSYZE" class="form-control newtouch_Readonly" />
                    </td>
                    <th></th>
                    <td></td>
                </tr>
            </table>
        </div>
        <div class="panel-heading">
            药品信息
        </div>
        <div id="divYPXX" style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="required">*</span>药品代码：</th>
                    <td class="formValue">
                        <input id="editRowId" value="" style="display:none;" />
                        <input type="text" id="txtYPCODE" class="form-control" />
                    </td>
                    <th class="formTitle">药品名称：</th>
                    <td class="formValue">
                        <input type="text" id="txtYPMC" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">批号：</th>
                    <td class="formValue">
                        <input type="text" id="txtPH" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">批次：</th>
                    <td class="formValue">
                        <input type="text" id="txtPC" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">有效期：</th>
                    <td class="formValue">
                        <input id="txtYXQ" type="text" class="form-control input-wdatepicker newtouch_Readonly" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                    </td>
                    <th class="formTitle">规格：</th>
                    <td class="formValue">
                        <input type="text" id="txtYPGG" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle" id="thXYKCDW">现有库存：</th>
                    <td class="formValue">
                        <input type="text" id="txtXYKC" class="form-control newtouch_Readonly" />
                        <input id="hidden_kcsl" type="text" hidden="hidden" />
                    </td>
                    <th class="formTitle">生产厂家：</th>
                    <td class="formValue">
                        <input type="text" id="txtSCCJ" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle" id="thPFJDW">批发价：</th>
                    <td class="formValue">
                        <input type="text" id="txtPFJ" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle" id="thPFZEDW">批发总额(元)：</th>
                    <td class="formValue">
                        <input type="text" id="txtPFZE" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle" id="thLSJDW">零售价：</th>
                    <td class="formValue">
                        <input type="text" id="txtLSJ" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle" id="thLSZEDW">零售总额(元)：</th>
                    <td class="formValue">
                        <input type="text" id="txtLSZE" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="panel-heading">
            单个损益信息
        </div>
        <div id="divDGSYXX" style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle" id="thSYSLDW" style="width: 80px;"><span class="required">*</span>损溢数量：</th>
                    <td class="formValue">
                        <div class="row">
                            <div hidden id="div_sy" class="col-md-4">
                                <input id="txtBMSL" type="text" class="form-control" onkeyup="SYSL_change(this)" />
                            </div>
                            <div hidden id="div_dw" class="col-md-2" style="padding: 3px 1px;">
                                <span id="bmdw"></span>
                            </div>
                            <div class="col-md-4">
                                <input id="txtZXSL" type="text" class="form-control" onkeyup="SYSL_change(this)" />
                            </div>
                            <div class="col-md-2" style="padding: 3px 1px;">
                                <span id="zxdw"></span>
                            </div>
                        </div>
                    </td>
                    <th class="formTitle">损溢原因：</th>
                    <td class="formValue">
                        <select id="ddlSYYY" class="form-control"></select>
                    </td>
                    <th class="formTitle" style="width: 60px;">备注：</th>
                    <td class="formValue">
                        <input type="text" id="txtRemark" class="form-control" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>责任人工号：</th>
                    <td class="formValue">
                        <input type="text" id="txtZRRGH" class="form-control" />
                        <input type="text" id="txtZRRMC" hidden />
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
@Html.Partial("_MiddleButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new int[] { 3, 6 },
    F3Text = "添加",
    F6Text = "删除",
})
<div style="background-color: #fff;width:99%;">
    <table id="gridSYXX"></table>
</div>
@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new int[] { 7, 8 },
    F7Text = "新损溢",
    F8Text = "提交"
})
<script>
    window.newtouch_globalconfig.f4opions = {
        container: "#divYPXX"
    };
    var Sysl = 0;

    $(function () {
        gridSYXX();//损益信息
        initialSYDH();//加载损益单号
        initialSYYY();//加载损益原因
    });

    $('#btnLossAndProfitTypeGourp').on('click', '.btn', function () {
        if (!$(this).hasClass('active')) {
            $(this).siblings('.btn').removeClass('btn-restore').removeClass('active').addClass('btn-default');
            $(this).removeClass('btn-default').addClass('btn-restore active');
        }
        initialSYYY();
    });

    //加载损益单号
    function initialSYDH() {
        $.najax({
            url: "/PrepareMedicine/initialSYDH?r=" + Math.random(),
            data: { djmc: '报损报溢单'},
            dataType: "text",
            async: false,
            success: function (data) {
                $("#txtSYDH").val(data);
            }
        });
    }

    //加载损益原因
    function initialSYYY() {
        $("#ddlSYYY").empty();
        var sylx = "";
        if ($('#btnLossAndProfitTypeGourp .btn.active').attr('data-keywordtype') === 'Loss') {
            sylx = "0";//0 报损，1 报溢
        } else {
            sylx = "1";
        }
        $("#ddlSYYY").bindSelect({
            url: "/PrepareMedicine/GetLossProfitReasonListByType?sylx=" + sylx + "&&r=" + Math.random(),
            id: "syyyId",
            text: "syyy"
        });
    }

    //责任人
    $("#txtZRRGH").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 200,
        width: 300,
        id: 'initialZRR',
        url: '/PrepareMedicine/GetZRRList',
        ajaxmethod: 'Get',
        ajaxreqdata: function () {
            var reqData = {};
            reqData.keyword = $("#txtZRRGH").val();
            return reqData;
        },
        caption: "责任人",
        colModel: [
            { label: '工号', name: 'gh', widthratio: 25 },
            { label: '名称', name: 'Name', widthratio: 25 },
            { label: '拼音', name: 'py', widthratio: 25 }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#txtZRRGH").val($thistr.attr('data-gh'));
            $("#txtZRRMC").val($thistr.attr('data-name'));
            return;
        }
    });

    //药品代码
    $("#txtYPCODE").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 200,
        width: 1130,
        id: 'initialYPCODE',
        url: '/PrepareMedicine/SelectLossAndProfitMedicineList',
        ajaxmethod: 'Get',
        ajaxreqdata: function () {
            var reqData = {};
            reqData.inputCode = $("#txtYPCODE").val();
            return reqData;
        },
        caption: "药品信息",
        colModel: [
            { label: '类别', name: 'yplb', width: 50 },
            { label: '名称', name: 'ypmc', width: 200, align: 'left' },
            { label: '库存', name: 'xykcstr', width: 70, align: 'right' },
            { label: '规格', name: 'ypgg', width: 150, align: 'center' },
            { label: '有效期', name: 'yxq', width: 80 },
            { label: '批号', name: 'ph', width: 110, align: 'center' },
            { label: '批次', name: 'pc', width: 140, align: 'center' },
            { label: '生产厂家', name: 'sccj', width: 220, align: 'center' },
            { label: '状态', name: 'ypzt', width: 50 },
            { label: '进价', name: 'jj', hidden: true },
            { label: '零售价', name: 'lsj', hidden: true },
            { label: '批发价', name: 'pfj', hidden: true },
            { label: '药库零售价', name: 'Yklsj', hidden: true },
            { label: '药库批发价', name: 'Ykpfj', hidden: true },
            { label: '代码', name: 'ypCode', hidden: true },
            { label: '转换因子', name: 'zhyz', hidden: true },
            { label: 'kcsl', name: 'kcsl', hidden: true },
            { label: 'zxdw', name: 'zxdw', hidden: true },
            { label: '单位', name: 'djdw', hidden: true }
        ],
        itemdbclickhandler: function ($thistr) {
            var djdw = $thistr.attr('data-djdw');
            var zxdw = $thistr.attr('data-zxdw');
            $("#txtYPCODE").attr('data-djdw', djdw).attr('data-zhyz', $thistr.attr('data-zhyz')).val($thistr.attr('data-ypCode'));
            $("#txtYPCODE").attr('data-jj', $thistr.attr('data-jj'));
            $("#txtYPMC").val($thistr.attr('data-ypmc'));
            $("#txtYPGG").val($thistr.attr('data-ypgg'));
            $("#txtPH").val($thistr.attr('data-ph'));
            $("#txtPC").val($thistr.attr('data-pc'));
            $("#txtYXQ").val($thistr.attr('data-yxq'));
            $("#txtSCCJ").val($thistr.attr('data-sccj'));
            $("#txtXYKC").attr('data-kcsl', $thistr.attr('data-kcsl')).val($thistr.attr('data-xykcstr'));
            $("#hidden_kcsl").val($thistr.attr('data-kcsl'));
            $("#thXYKCDW").text('现有库存：');
            $("#txtPFJ").val(parseFloat($thistr.attr('data-pfj')).toFixed(4)).attr("data-Ykpfj", $thistr.attr('data-Ykpfj'));
            $("#thPFJDW").text('批发价(元/' + zxdw + ')：');
            $("#txtLSJ").val(parseFloat($thistr.attr('data-lsj')).toFixed(4)).attr("data-Yklsj", $thistr.attr('data-Yklsj'));;
            $("#thLSJDW").text('零售价(元/' + zxdw + ')：');
            $("#thSYSLDW").text('损溢数量：');
            $("#txtBMSL").val('');
            $("#txtZXSL").val('');
            if (djdw === zxdw) {
                $("#zxdw").html(zxdw);
                $("#div_sy").hide();
                $("#div_dw").hide();
            } else {
                $("#bmdw").html(djdw);
                $("#zxdw").html(zxdw);
                $("#div_sy").show();
                $("#div_dw").show();
            }
            return;
        }
    });

    //损益信息
    function gridSYXX() {
        var $gridSyxx = $("#gridSYXX");
        $gridSyxx.dataGrid({
            unwritten: false,
            height: $(window).height() - document.getElementById('gridSYXX').offsetTop - 85,
            colModel: [
                { label: '损溢情况', name: 'syqk', width: 70, align: 'left' },
                { label: '药品代码', name: 'ypdm', width: 70, align: 'left' },
                { label: '药品名称', name: 'ypmc', width: 100, align: 'left' },
                { label: '规格', name: 'ypgg', width: 100, align: 'left' },
                { label: '损益数量', name: 'sl', width: 100, align: 'left' },
                { label: '单位', name: 'dw', width: 100, align: 'left' },
                { label: '现有库存', name: 'xykc', width: 100, align: 'left' },
                { label: '批号', name: 'ph', width: 100, align: 'left' },
                { label: '批次', name: 'pc', width: 100, align: 'left' },
                { label: '转换因子', name: 'zhyz', width: 100, align: 'left' },
                { label: '有效期', name: 'yxq', width: 100, align: 'left' },
                { label: '生产厂家', name: 'sccj', width: 100, align: 'left' },
                { label: '批发价', name: 'pfj', width: 100, align: 'left' },
                { label: '零售价', name: 'lsj', width: 100, align: 'left' },
                { label: '损益原因', name: 'syyymc', width: 100, align: 'left' },
                { label: '备注', name: 'remark', width: 100, align: 'left' },
                { label: '责任人', name: 'zrr', width: 100, align: 'left' },
                { label: '单据号', name: 'djh', width: 100, align: 'left' },
                { label: 'Jj', name: 'Jj', hidden: true },
                { label: 'Yklsj', name: 'Yklsj', hidden: true },
                { label: 'Ykpfj', name: 'Ykpfj', hidden: true },
                { label: '损益原因', name: 'syyy', hidden: true },
                { label: '库存数量', name: 'kcsl', hidden: true },
                { label: '损益数量', name: 'sysl', hidden: true },
                { label: '批发价单位', name: 'pfjdw', hidden: true },
                { label: '零售价单位', name: 'lsjdw', hidden: true },
                { label: 'lsze', name: 'lsze', hidden: true },
                { label: '责任人', name: 'zrrmc', hidden: true },
                { label: 'pfze', name: 'pfze', hidden: true }

            ],
            ondblClickRow: function (rowid) {
                //if (rowid) {
                //    var getRowData = $("#gridSYXX").jqGrid('getRowData', rowid);
                //    if (getRowData) {
                //        if (getRowData.syqk === "报损") {  //报损
                //            if (!$("#btnLoss").hasClass('active')) {
                //                $("#btnProfit").removeClass('btn-restore').removeClass('active');
                //                $("#btnLoss").addClass('btn-restore active');
                //            }
                //        } else {   //报溢
                //            if (!$("#btnProfit").hasClass('active')) {
                //                $("#btnLoss").removeClass('btn-restore').removeClass('active');
                //                $("#btnProfit").addClass('btn-restore active');
                //            }
                //        }
                //        $("#txtYPCODE").attr("data-zhyz", getRowData.zhyz).val(getRowData.ypdm);
                //        $("#txtYPMC").val(getRowData.ypmc);
                //        $("#txtYPGG").val(getRowData.ypgg);
                //        $("#txtPH").val(getRowData.ph);
                //        $("#txtPC").val(getRowData.pc);
                //        $("#txtYXQ").val(getRowData.yxq);
                //        $("#txtSCCJ").val(getRowData.sccj);
                //        $("#txtXYKC").val(getRowData.xykc);
                //        $("#thXYKCDW").val(getRowData.xykcdw);
                //        $("#txtLSJ").val(getRowData.lsj);
                //        $("#thLSJDW").val(getRowData.lsjdw);
                //        $("#txtPFJ").val(getRowData.pfj);
                //        $("#thPFJDW").val(getRowData.pfjdw);
                //        $("#thSYSLDW").val(getRowData.sysldw);
                //        $("#txtLSZE").val(getRowData.lsze);
                //        $("#txtPFZE").val(getRowData.pfze);
                //        $("#hidden_kcsl").val(getRowData.kcsl);
                //        $('#editRowId').val(rowid);
                //        $('#txtRemark').val(getRowData.remark);
                //        $("#ddlSYYY").val(getRowData.syyy);
                //    }
                //}
            }
        });
    }

    //确定button
    function newtouch_event_f3() {
        //检查明细
        if (!CheckDetailData()) {
            return false;
        }
        var syqkstr = "";
        var sysl = Sysl;//最后保存到损益表

        //判断报损报溢
        if ($('#btnLossAndProfitTypeGourp .btn.active').attr('data-keywordtype') === 'Loss') {
            syqkstr = "报损",
                sysl = sysl * (-1);
        } else {
            syqkstr = "报溢";
        }

        var data = [{
            syqk: syqkstr,
            ypdm: $("#txtYPCODE").val(),
            ypmc: $("#txtYPMC").val(),
            ypgg: $("#txtYPGG").val(),
            sccj: $("#txtSCCJ").val(),
            ph: $("#txtPH").val(),
            pc: $("#txtPC").val(),
            yxq: $("#txtYXQ").val(),
            sysl: sysl,//最后保存到损益表
            sl: Sysl,//页面上显示损益数量
            dw: $("#zxdw").text(),
            xykc: $("#txtXYKC").val(),
            pfj: $("#txtPFJ").val(),
            Ykpfj: parseFloat($("#txtPFJ").attr("data-Ykpfj")),
            pfjdw: $("#thPFJDW").val(),
            pfze: $("#txtPFZE").val(),
            lsj: $("#txtLSJ").val(),
            Yklsj: parseFloat($("#txtLSJ").attr("data-Yklsj")),
            lsjdw: $("#thLSJDW").val(),
            lsze: $("#txtLSZE").val(),
            Jj: parseFloat($("#txtYPCODE").attr("data-jj")),
            syyymc: $("#ddlSYYY").find("option:selected").text(),
            syyy: $("#ddlSYYY").val(),
            zhyz: $("#txtYPCODE").attr("data-zhyz"),
            zrrmc: $("#txtZRRMC").val(),
            zrr: $("#txtZRRGH").val(),
            djh: $("#txtSYDH").val(),
            kcsl: $("#hidden_kcsl").val(),
            remark: $("#txtRemark").val()
        }];

        if ($('#editRowId').val()) {            //是修改
            $('#gridSYXX').jqGrid("delRowData", $('#editRowId').val()); //先remove掉那一行
        }

        //检查重复项
        var isRepeatedAdd = false;
        $.each($("#gridSYXX").getDataIDs(), function () {
            var rowData = $("#gridSYXX").jqGrid('getRowData', this);
            if (rowData.ypdm === data[0].ypdm && rowData.ph === data[0].ph && rowData.pc === data[0].pc) {
                isRepeatedAdd = true;
            }
        });

        if (isRepeatedAdd) {
            $.modalAlert("不可重复添加", 'warning');
            return false;
        }

        $("#gridSYXX").newtouchLocalDataGrid(null, data);
        //计算损益总额
        ComputeRKZE();
        $("#btnLoss").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
        $("#btnProfit").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
        ClearYPXX();
    }

    //删除
    function newtouch_event_f6() {
        var delRowid = $("#gridSYXX").jqGrid('getGridParam', 'selrow');
        if (!delRowid) {
            $.modalAlert("未选中行", 'warning');
            return;
        }
        $('#gridSYXX').jqGrid('delRowData', delRowid);
    }

    //保存
    function newtouch_event_f8() {
        var syxx = $("#gridSYXX").jqGrid('getRowData_AllLine');
        if (syxx == null || syxx.length <= 0) {
            $.modalAlert("请先添加报损报溢信息", 'warning');
            return;
        }
        $.najax({
            url: "/PrepareMedicine/SubmitReportLossAndProfit",
            type: "POST",
            data: { syxx: syxx },
            dataType: "json",
            success: function (data) {
                $.modalMsg("保存成功", 'success', 1000);
                newtouch_event_f7();//新损溢：新单据号 主数据清空 明细清空
                $('#gridSYXX').jqGrid("clearGridData");//清空Grid
				$("#txtSYZE").val("");//清空损益总额
				window.open("/Report/PrintReport?type=报损报溢打印表&param=syIds=" + data.message +"|OrganizeId=" + $("#OrganizeId").val(),"height=500, width=1195, top=100, left=50, toolbar=no, menubar=no, scrollbar=yes, resizeble=no, location=no, status=no");
            }
        });
    }

    //新损益
    function newtouch_event_f7() {
        initialSYDH();//换单号
        $("#btnLoss").removeAttr("disabled").removeAttr("style");
        $("#btnProfit").removeAttr("disabled").removeAttr("style");
    }

    //损益数量keyup事件
    function SYSL_change(obj) {
        if ($("#txtYPCODE").val() === "") {
            return false;
        }
        obj.value = obj.value.replace(/\D/g, '');
        var bmsl = $.trim($("#txtBMSL").val());
        var zxsl = $.trim($("#txtZXSL").val());
        if (bmsl === "" && zxsl === "") {
            return false;
        }
        bmsl = bmsl === '' ? 0 : bmsl;
        zxsl = zxsl === '' ? 0 : zxsl;
        if ($("#div_sy").is(":hidden")) {   //单价单位(当前部门的单位) sl/zhyz
            Sysl = parseFloat(zxsl);
        } else {
            Sysl = parseFloat(bmsl) * parseFloat($("#txtYPCODE").attr('data-zhyz')) + parseFloat(zxsl);
        }
        var pfj = parseFloat($("#txtPFJ").val());
        var lsj = parseFloat($("#txtLSJ").val());

        $("#txtPFZE").val((pfj * Sysl).toFixed(4));
        $("#txtLSZE").val((lsj * Sysl).toFixed(4));
    }

    //计算损益总额
    function ComputeRKZE() {
        var syze = 0;
        var count = $("#gridSYXX").getGridParam('records');
        if (count > 0) {
            var rowIDS = $("#gridSYXX").getDataIDs();
            for (var i = 0; i < rowIDS.length; i++) {
                var getRow_lsze = $("#gridSYXX").getRowData(rowIDS[i]).lsze;
                syze = parseFloat(syze) + parseFloat(getRow_lsze);
            }
            //损益金额
            $("#txtSYZE").val(syze.toFixed(4));
        } else {
            $("#txtSYZE").val('');
        }
    }

    //清空药品信息
    function ClearYPXX() {
        //清空
        newtouch_globalevent_f4();
        $("#ddlSKDW").empty();
        $("#txtSYSL").val("");
        $("#txtZXSL").val("");
        $("#txtBMSL").val("");
        $("#txtYXQ").val($.getDate());
    }

    //校验明细
    function CheckDetailData() {
        if ($("#txtYPCODE").val() == "") {
            $.modalAlert("药品代码为空", 'warning');
            return false;
        }
        if ($("#txtYPMC").val() == "") {
            $.modalAlert("药品名称为空", 'warning');
            return false;
        }
        if ($("#txtPC").val() === "") {
            $.modalAlert("批次为空", 'warning');
            return false;
        }
        if ($("#txtBMSL").val() == "" && $("#txtZXSL").val() == "") {
            $.modalAlert("损溢数量为空", 'warning');
            return false;
        }
        if (Sysl <= 0) {
            $.modalAlert("损溢数量应大于0", 'warning');
            return false;
        }
        if ($('#btnLossAndProfitTypeGourp .btn.active').attr('data-keywordtype') == 'Loss') {
            if (Sysl > parseFloat($("#txtXYKC").attr('data-kcsl'))) {   //反之，直接比较数量（最小单位数量）
                $.modalAlert("报损数量应小于现有库存", 'warning');
                return false;
            }
        }
        if ($("#ddlSYYY").val() == "") {
            $.modalAlert("损溢原因为空", 'warning');
            return false;
        }
        if ($("#txtZRRGH").val() == "") {
            $.modalAlert("责任人工号为空", 'warning');
            return false;
        }
        if ($("#txtZRRMC").val() == "") {
            $.modalAlert("责任人名称为空", 'warning');
            return false;
        }
        return true;
    }
</script>
